package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wk
 * @create 2021-09-13 22:51
 */
@Configuration
@Component
public class DelayedConfig {
    public static final String exchange_delay = "exchange.delay";
    public static final String routing_delay = "routing.delay";
    public static final String queue_delay_1 = "queue.delay.1";

    /**
     * fanout 是发布订阅模式的交换机
     * direct 是路由模式的交换机
     * topic 是主题模式的交换机
     */
    // 基于插件的交换机 自定义交换机
    @Bean
    public Exchange DelayedExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type", "direct"); // 使用路由模式的交换机
        return new CustomExchange(exchange_delay, "x-delayed-message", true, false, arguments);
    }

    @Bean
    public Queue DelayedQueue() {
        return QueueBuilder.durable(queue_delay_1).build();
    }

    @Bean
    public Binding binding(@Qualifier(value = "DelayedQueue") Queue queue,
                           @Qualifier(value = "DelayedExchange")Exchange delayedExchange) {
        return BindingBuilder.bind(queue).to(delayedExchange).with(routing_delay).noargs();
    }
}
